package models

type CookingTools struct {
	ToolID      uint64    `gorm:"column:tool_id;primary_key;auto_increment" json:"tool_id"`
	Name        string    `gorm:"column:name;not null" json:"name" `
	Description string    `gorm:"column:description;not null" json:"description"`
	ImageURL    string    `gorm:"column:image_url;not null" json:"image_url" `
	Category    string    `gorm:"column:category;not null" json:"category" `
	Recipes     []Recipes `gorm:"many2many:recipes_tool_map"`
}

func (tools *CookingTools) Delete() error {
	return DB.Self.Delete(&tools).Error
}
func (tools *CookingTools) Create() error {
	return DB.Self.Create(&tools).Error
}

func (tool *CookingTools) Update() error {
	return DB.Self.Model(&tool).Update(&tool).Error
}

// 查询，提供Id查询
func GetCookingToolsById(id uint64) (*CookingTools, error) {
	tool := &CookingTools{}
	r := DB.Self.Preload("Recipes").First(tool, id)
	return tool, r.Error
}

// 查询所有
func GetTools() ([]*CookingTools, error) {
	tools := make([]*CookingTools, 0)
	err := DB.Self.Find(&tools).Error
	return tools, err
}
